﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Garlic.CC.BaseClass;
using BLL.SystemManagement;
using System.Data;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxEditors;

namespace Garlic.BasicInfo
{
    public partial class FlightInfo : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["mainPage"] = Request.Url; //记住当前主页面地址，防止F5
                BindShipAgency();
                ServiceRouteTree1.iuser = CurrentUser; //当前用户
            }
            gridFlightInfo.Enabled = true;
            BindData();
        }

        //获取船代理
        public void BindShipAgency()
        {
            DataTable customer = FlightInfoBLL.GetShipAgency(CurrentUser.UserID);
            txtCst_cstmnm.Text = Convert.ToString(customer.Rows[0]["cst_cstmnm"]);
            hidCst_id.Value = Convert.ToString(customer.Rows[0]["cst_id"]);
        }

        //预分码头
        protected void btnFlt_schdul_Click(object sender, EventArgs e)
        {

            Flt_schdul();
        }

        private void Flt_schdul()
        {

            int flt_sln_id = 0;// Convert.ToInt32(ServiceRouteTree1.ServiceRouteID); 
            int.TryParse(ServiceRouteTree1.ServiceRouteID.ToString(), out flt_sln_id);
            if (flt_sln_id != 0)
            {
                DataTable flt_schdul = FlightInfoBLL.GetFlt_schdulDataTable(flt_sln_id);
                ddlFlt_schdul.DataTextField = "cst_cstmcd";//预分码头简称
                ddlFlt_schdul.DataValueField = "FLT_TER_CST_ID";//预分码头ID
                ddlFlt_schdul.DataSource = flt_schdul;
                ddlFlt_schdul.DataBind();

                // ddlTerminal.SelectedValue = ddlFlt_schdul.SelectedValue;
            }
            else
            {
                ddlFlt_schdul.Items.Clear();
                // ddlTerminal.SelectedValue="";
            }
        }

        protected void ddlFlt_schdul_SelectedIndexChanged(object sender, EventArgs e)
        {
            //ddlTerminal.SelectedValue = ddlFlt_schdul.SelectedValue;
        }

        protected void ButtonSubmit_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            string flt_sln_id = ServiceRouteTree1.ServiceRouteID;
            string flt_ter_cst_id = ddlFlt_schdul.SelectedValue;


            int? agentID = Int32.Parse(hidCst_id.Value);
            if (CurrentUser.UserType == "1")
                agentID = null;

            gridFlightInfo.DataSource = FlightInfoBLL.GetSt_flightsDataTable(agentID, flt_sln_id, flt_ter_cst_id);
            gridFlightInfo.DataBind();
            gridFlightInfo.KeyFieldName = "FLT_ID";
        }

        protected void gridFlightInfo_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
            try
            {
                FlightInfoBLL.Insert_St_flights(hiddenSlnName.Value, hdnCSTM.Value,
                     e.NewValues["FLT_SCHDUL"].ToString(), e.NewValues["FLT_CYCLE"].ToString(),
                     e.NewValues["FLT_INITDT"].ToString(),
                     e.NewValues["FLT_DOCKHOUR"] == null ? "" : e.NewValues["FLT_DOCKHOUR"].ToString(),
                     e.NewValues["FLT_IAMOUNT"] == null ? "" : e.NewValues["FLT_IAMOUNT"].ToString(),
                     e.NewValues["FLT_EAMOUNT"] == null ? "" : e.NewValues["FLT_EAMOUNT"].ToString(),
                     CurrentUser.AccountName
                    );

                e.Cancel = true;
                gridFlightInfo.CancelEdit();
                BindData();
            }
            catch (Exception e1)
            {
                throw e1;
            }

        }

        protected void gridFlightInfo_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
        {
            // #Update DB
            FlightInfoBLL.UpdateSt_flights(Convert.ToInt32(e.Keys[0]),
                
                e.NewValues["FLT_CYCLE"].ToString(),
                e.NewValues["FLT_INITDT"].ToString(),
                e.NewValues["FLT_DOCKHOUR"] == null ? "" : e.NewValues["FLT_DOCKHOUR"].ToString(),
                e.NewValues["FLT_IAMOUNT"] == null ? "" : e.NewValues["FLT_IAMOUNT"].ToString(),
                e.NewValues["FLT_EAMOUNT"] == null ? "" : e.NewValues["FLT_EAMOUNT"].ToString(),
                CurrentUser.AccountName);
            e.Cancel = true;
            gridFlightInfo.CancelEdit();
            BindData();

        }

        protected void gridFlightInfo_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
        {
            try
            {
                FlightInfoBLL.Delete_St_flights(Convert.ToInt32(e.Keys[0]));
                e.Cancel = true;
                gridFlightInfo.CancelEdit();
                BindData();
            }
            catch (Exception e1)
            {
                throw new Exception("删除失败！");
            }

        }


        protected void gridFlightInfo_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
        {

            if (!gridFlightInfo.IsEditing)
                return;


            if (e.Column.FieldName == "SRT_RTCHNNM")
            {
                ASPxComboBox combo = e.Editor as ASPxComboBox;
                FlightsVessels.Fill_srt_rtchnnmCombo(combo, CurrentUser);
            }

            if (e.Column.FieldName == "SLN_NAME")
            {
                ASPxComboBox combo = e.Editor as ASPxComboBox;
                combo.Callback += new DevExpress.Web.ASPxClasses.CallbackEventHandlerBase(combo_Callback);
                if (combo.Value != null)
                {
                    hiddenSlnName.Value = combo.Value.ToString();
                }
                if (gridFlightInfo.IsNewRowEditing)
                {
                    return;
                }
                object val = gridFlightInfo.GetRowValuesByKeyValue(e.KeyValue, "SRT_RTCHNNM");

                if (val == DBNull.Value || val == null) return;
                string srt_rtchnnm = (string)val;
                Fill_flt_sln_rtchnnmCombo(combo, srt_rtchnnm);


            }

            if (e.Column.FieldName == "FLT_SCHDUL")
            {
                ASPxComboBox cmbSchdul = e.Editor as ASPxComboBox;

                Fill_SchualCmb(cmbSchdul);


            }

            if (e.Column.FieldName == "CST_CSTMCD")
            {
                //预分码头cmb
                ASPxComboBox cmbCSTM = e.Editor as ASPxComboBox;
                cmbCSTM.Callback += new DevExpress.Web.ASPxClasses.CallbackEventHandlerBase(cmbCSTM_Callback);

                if (cmbCSTM.Value != null)
                {
                    hdnCSTM.Value = cmbCSTM.Value.ToString();
                }
                if (gridFlightInfo.IsNewRowEditing)
                {
                    return;
                }
                object val = gridFlightInfo.GetRowValuesByKeyValue(e.KeyValue, "SLN_ID");

                if (val == DBNull.Value || val == null) return;
                string sln_ID = val.ToString();
                FillCSTM_By_SLN_ID(cmbCSTM, sln_ID);

            }



        }

        void cmbCSTM_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            FillCSTM_By_SLN_ID(sender as ASPxComboBox, e.Parameter);
        }

        void combo_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            FlightsVessels.Fill_flt_sln_rtchnnmCombo1(sender as ASPxComboBox, e.Parameter, CurrentUser);
        }

        protected void gridFlightInfo_StartRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e)
        {
            // gridFlightInfo.Templates.EditForm.
        }

        /// <summary>
        /// 根据服务航线ID填充预分码头
        /// </summary>
        public void FillCSTM_By_SLN_ID(ASPxComboBox cmb, string id)
        {
            string agentID = null;

            if (CurrentUser.UserType == "1")
                agentID = null;
            else
            {
                agentID = hidCst_id.Value;
            }

            DataTable dt = FlightInfoBLL.Get_CST_NameBySLN(id, agentID);

            if (dt != null)
            {
                cmb.DataSource = dt;
                cmb.ValueField = "cst_id";
                cmb.TextField = "cst_cstmcd";
                cmb.DataBind();
            }
        }

        public void Fill_SchualCmb(ASPxComboBox cmb)
        {
            cmb.Items.Clear();
            DataTable dt = FlightInfoBLL.GetFLT_SCHDUL();
            cmb.DataSource = dt;
            cmb.ValueField = "RV_LOW_VALUE";
            cmb.TextField = "RV_LOW_VALUE";
            cmb.DataBind();
        }

        /// <summary>
        /// 根据一级航线填充二级航线
        /// </summary>
        /// <param name="cmb"></param>
        /// <param name="srt_rtchnnm"></param>
        /// <param name="user"></param>
        public void Fill_flt_sln_rtchnnmCombo(ASPxComboBox cmb, string srt_rtchnnm)
        {
            if (srt_rtchnnm == "") return;
          //  cmb.Items.Clear();

            DataTable dt = ServiceRouteTreeBLL.GetRoutes2(CurrentUser.UserID, CurrentUser.UserType, ServiceRouteTreeBLL.GetSt_sea_routesBysrt_id(srt_rtchnnm));
           
            cmb.DataSource = dt;
            cmb.TextField = "sln_name";
            cmb.ValueField = "sln_id";
            cmb.DataBind();

          


        }


    }
}